import App from './App'
import store from '@/vuex/store.js'
import publicView from "@/components/publicView/index.vue"
import location from "./location.js"
import config from "@/utils/config.js"
import {
	util
} from "/utils/request.js"
import {
	createSSRApp
} from 'vue'
export function createApp() {
	const app = createSSRApp(App)
	app.component("public-view", publicView)
	app.config.globalProperties.util = util
	app.config.globalProperties.$location = location
	app.config.globalProperties.$setNotice = ()=>{
		util.postRequest({
			url:"v1/user/unread"
		}).then(res=>{
			console.log(res)
			if(res.data.message>0 || res.data.notice_count>0){
				store.commit("changeshowNotice",true)
			}else{
				store.commit("changeshowNotice",false)
			}
		})
	}
	// #ifdef APP-PLUS
	app.config.globalProperties.$uploadApp = (callback)=>{
		util.postRequest({
			url:"v1/public_/checkUpdate",
			data:{
				version_name:config.version_name,
				version_code:config.version_code
			}
		}).then(res=>{
			// uni.setStorageSync("Version",res.data.version.version)
			callback(res)
		})
	}
	// #endif
	app.config.globalProperties.$readyMsg = (id)=>{
		util.postRequest({
			url:"v1/message/info",
			data:{
				id
			}
		}).then(res=>{
			console.log(res,"readyMsg")
		})
	}
	app.config.globalProperties.$createPay = function (data={},callback){
		console.log(data,1111111)
		util.postRequest({
			url:"v1/order/pay",
			data
		}).then(res=>{
			let dataa = {}
			if(data.pay_type === 'balance'){
				uni.showToast({
					title:"支付成功"
				})
				callback(res)
				return
			}
			if(data.pay_type === 'alipay'){
				dataa.provider = "alipay"
				dataa.orderInfo = res.data.pay_order
			}
			if(data.pay_type === 'wechat'){
				dataa.provider = "wxpay"
				dataa.orderInfo = res.data.pay_order
			}
			console.log(dataa)
			uni.requestPayment({
				...dataa,
				success(sres){
					console.log(sres,"ffffffff")
					uni.showToast({
						title:"支付成功",
					})
					callback(res)
				},
				fail(e) {
					callback(false)
					uni.showToast({
						title:"支付失败",
						icon:"none"
					})
				},
				complete(e) {
					console.log(e)
				}
			})
		}).catch(e=>{
			console.log(e,1111111)
			callback(false)
			uni.showToast({
				title:e.msg||"请求失败",
				icon:'none'
			})
		})
	}
	app.config.globalProperties.$navLocation = (data={})=>{
		uni.openLocation({
			latitude:Number(data.lat),
			longitude:Number(data.lng)
		})
	},
	app.config.globalProperties.$callPhone = (phone)=>{
		// if(phone.length)
		if(phone.length<=0){
			uni.showToast({ 
				title:"电话号码不正确",
				icon:"none"
			})
			return
		}
		uni.makePhoneCall({
			phoneNumber:phone,
			fail(e) {
				console.log(e)
				if(e.errMsg.indexOf("cancel")<=-1){
					uni.showToast({
						title:"电话号码不正确",
						icon:"none"
					})
				}
			}
		})
	}
	app.config.globalProperties.$getDefaultAddress = (callback)=>{
		if(typeof callback === 'function'){
			util.postRequest({
				url:"v1/user_address/default",
			}).then(res=>{
				console.log(res)
				callback(res)
			})
		}
	}
	app.config.globalProperties.$getAreaByCoordinate = (lng,lat,level,isState=true,callback = null)=>{
		util.postRequest({
			url:"v1/public_/getAreaByCoordinate",
			data:{
				lng,
				lat,
				level
			}
		}).then(res=>{
			if(isState){
				store.commit("changenav_address",{
					provinces:res.data[0],
					city:res.data[1],
					area:res.data[2]
				})
			}else{
				callback({
					provinces:res.data[0],
					city:res.data[1],
					area:res.data[2]
				})
			}
		})
	}
	app.config.globalProperties.$getUser = () => {
		const promist = util.postRequest({
			url: "v1/user/index"
		}).then(res => {
			uni.hideLoading()
			let user = uni.getStorageSync("userInfo")
			let token = user.token
			user = res.data
			user.token = token
			uni.setStorageSync("userInfo",user)
			store.commit("changeUserInfo", user)
		}).catch((e)=>{
			if(e.code === 401){
				store.commit("changeLogin")
			}
			uni.hideLoading()
		})
		// return promist
	}
	app.config.globalProperties.$prviceImage = (img)=>{
		uni.previewImage({
			urls:img,
			fail(e) {
				console.log(e)
			}
		})
	}
	app.config.globalProperties.$getCarTypeList = ()=>{
		uni.showLoading({
			title:"请稍等..."
		})
		util.getRequest({
			url:"v1/public_/getModel",
		}).then(res=>{
			uni.hideLoading()
			store.commit("changeSendCarMode",res.data)
		})
	}
	app.config.globalProperties.$getLeve3Address = (lat,lng,success)=>{
		uni.showLoading({
			title:"请稍等..."
		})
		util.postRequest({
			url:"v1/public_/getAreaByCoordinate",
			data:{
				lng,
				lat
			}
		}).then(res=>{
			uni.hideLoading()
			success(res.data)
		}).catch(e=>{
			uni.hideLoading()
		})
	}
	app.config.globalProperties.$getArea = async (pid = 0,deep=0) => {
		return new Promise((resolve,reject)=>{
			let data = {}
			if(pid !== 0){
				data.pid = pid
			}
			util.postRequest({
				url: "v1/public_/getAreaList",
				data
			}).then(res => {
				uni.hideLoading()
				if(deep === 0){
					store.commit("changeProvinces",res.data)
				}
				if(deep === 1){
					store.commit("changeCity",res.data)
				}
				if(deep === 2){
					store.commit("changeArea",res.data)
				}
				resolve(res.data)
			}).catch((e)=>{
				uni.hideLoading()
			})
		})
	}
	app.config.globalProperties.$store = store
	app.use(store)
	return {
		app
	}
}